//
//  MainViewController.swift
//  我的微博
//
//  Created by ZhangChao on 15/8/31.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController()
//        // 但是在这里加上按钮,点不到按钮,只能点到tabBar的item
//        setupComposeButton()
    }
    
    // 所以要在这个方法中添加
    override func viewWillAppear(animated: Bool) {
        
        // 这里记得调用父类的方法,要不然会造成按钮无法点击
        super.viewWillAppear(animated)
        // 在这个方法中tabBar才会添加item 所以要再这里设置加号按钮
        
        setupComposeButton()
    }
    
    
    
    // 单独添加控制器
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        let vc = vc
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

    // 添加所有子控制器
    private func addChildViewController() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MassageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    // 添加加号按钮
    private lazy var composedButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(btn)
        
        return btn
        
    }()
    
    // 添加按钮,并设置frame
    private func setupComposeButton(){
        
        // 获取总按钮的个数
        let count = childViewControllers.count
        
        //计算每个按钮的宽度
        let w = tabBar.bounds.width / CGFloat(count)

        composedButton.frame = CGRect(x: w*2, y: 0, width: w, height: tabBar.bounds.height)
        
    }
    
    // 监听按钮的点击事件
    @objc private func composeButtonClick(){
        
        let vc : UIViewController
        
        if UserAccountViewModel.sharedUserAccount.userLogon {
            
            vc = ComposeViewController()
        }else{
            
            vc = OAuthViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
}
